﻿using System;
using fastCSharp.code.cSharp;
using fastCSharp;

namespace diantou.web
{
    /// <summary>
    /// 全局视图
    /// </summary>
    /// <typeparam name="viewType">视图类型</typeparam>
    abstract class view<viewType> : webView.view<viewType>
         where viewType : view<viewType>
    {
        /// <summary>
        /// 公共路径
        /// </summary>
        protected diantou.dataModel.path.pub pubPath
        {
            get { return requestHeader.IsReView ? diantou.dataModel.path.pub.ViewOnlyPath : diantou.dataModel.path.pub.Default; }
        }
        /// <summary>
        /// 公共视图
        /// </summary>
        protected struct pub
        {
            /// <summary>
            /// 是否重新加载视图
            /// </summary>
            [fastCSharp.code.cSharp.webView.outputAjax]
            public bool ViewOnly;
            /// <summary>
            /// 问题数量
            /// </summary>
            public int ProblemCount
            {
                get
                {
                    if (ViewOnly) return 0;
                    return diantou.dataAccess.tcpCall.problem.getShowCount();
                }
            }
            /// <summary>
            /// 比赛数量
            /// </summary>
            public int ContestCount
            {
                get
                {
                    if (ViewOnly) return 0;
                    return diantou.dataAccess.tcpCall.contest.getCount();
                }
            }
            /// <summary>
            /// 教程数量
            /// </summary>
            public int TutorialCount
            {
                get
                {
                    if (ViewOnly) return 0;
                    return diantou.dataAccess.tcpCall.tutorial.getCount();
                }
            }
        }
        /// <summary>
        /// 公共视图
        /// </summary>
        protected pub pubView
        {
            get { return new pub { ViewOnly = requestHeader.IsReView }; }
        }
        /// <summary>
        /// 比赛计数是否有效
        /// </summary>
        private byte isContestCounter;
        /// <summary>
        /// 比赛计数
        /// </summary>
        private diantou.dataAccess.contest.counter contestCounter;
        /// <summary>
        /// 比赛计数
        /// </summary>
        internal diantou.dataAccess.contest.counter ContestCounter
        {
            get
            {
                if (isContestCounter == 0)
                {
                    contestCounter = diantou.dataAccess.tcpCall.contest.getCounter();
                    isContestCounter = 1;
                }
                return contestCounter;
            }
        }
        /// <summary>
        /// 通知标识
        /// </summary>
        [fastCSharp.code.cSharp.webView.query]
        protected int noticeId;
        /// <summary>
        /// 通知信息
        /// </summary>
        protected diantou.dataAccess.notice noticeMessage
        {
            get
            {
                if (currentUserId != 0 && noticeId != 0) return diantou.dataAccess.tcpCall.notice.getRead(noticeId, currentUser.Id, currentUser.IsChangeVisit);
                return null;
            }
        }
        /// <summary>
        /// 页面分页信息
        /// </summary>
        protected pageInfo pageInfo;
        /// <summary>
        /// 长连接轮询验证
        /// </summary>
        [fastCSharp.code.cSharp.webView.outputAjax]
        protected string pollVerify
        {
            get
            {
                return currentUser != null ? poll.Poll.Get(currentUser.Id).Verify : null;
            }
        }
        /// <summary>
        /// 当前用户
        /// </summary>
        private diantou.dataAccess.user currentUserValue;
        /// <summary>
        /// 当前用户
        /// </summary>
        protected diantou.dataAccess.user currentUser
        {
            get
            {
                if (currentUserValue == null)
                {
                    currentUserValue = web.pub.GetCurrentUser(this);
                    if (currentUserValue == null) currentUserValue = diantou.dataAccess.user.NullUser;
                }
                return currentUserValue != diantou.dataAccess.user.NullUser ? currentUserValue : null;
            }
        }
        /// <summary>
        /// 当前用户标识
        /// </summary>
        protected int currentUserId
        {
            get
            {
                return (currentUser ?? diantou.dataAccess.user.NullUser).Id;
            }
        }
        /// <summary>
        /// 清除页面信息
        /// </summary>
        protected override void clear()
        {
            base.clear();
            currentUserValue = null;
            isContestCounter = 0;
        }
    }
}
